package org.example.gxpt.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.gxpt.common.constant.JwtClaimsConstant;
import org.example.gxpt.common.constant.RoleConstant;
import org.example.gxpt.common.properties.JwtProperties;
import org.example.gxpt.common.result.PageResult;
import org.example.gxpt.common.result.Result;
import org.example.gxpt.common.utils.JwtUtil;
import org.example.gxpt.mapper.SystemAdminMapper;
import org.example.gxpt.pojo.dto.*;
import org.example.gxpt.pojo.entity.SystemAdmin;
import org.example.gxpt.pojo.vo.SystemAdminLoginVO;
import org.example.gxpt.service.SystemAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/systemAdmin")
@Slf4j
@Api(tags = "系统管理员相关接口")
@CrossOrigin
public class SystemAdminController {

    @Autowired
    private SystemAdminService systemAdminService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 系统管理员登陆
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("系统管理员登陆")
    public Result systemAdminLogin(@RequestBody LoginDTO loginDTO){
            SystemAdminLoginVO systemAdminLoginVO = systemAdminService.login(loginDTO);
        return Result.success(systemAdminLoginVO);
    }
    /**
     * 系统管理员注册
     * @param systemAdminDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("系统管理员注册")
    public Result systemAdminRegister(@RequestBody SystemAdminDTO systemAdminDTO){

        systemAdminService.save(systemAdminDTO);
        return Result.success();
    }

    /**
     * 系统管理员数据更新
     * @param systemAdminUpdateDTO
     * @return
     */
    @PutMapping("/updateById")
    @ApiOperation("根据id进行系统管理员数据更新")
    public Result updateSystemAdminById(@RequestBody SystemAdminUpdateDTO systemAdminUpdateDTO){

        systemAdminService.updateById(systemAdminUpdateDTO);
        return Result.success();
    }

    @PutMapping("/updateByContext")
    @ApiOperation("修改当前登录用户的数据")
    public Result updateSystemAdminByContext(@RequestBody SystemAdminUpdateDTO systemAdminUpdateDTO){
        systemAdminService.updateByContext(systemAdminUpdateDTO);
        return Result.success();
    }

    /**
     * 系统管理员分页查询
     * @param systemAdminPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("系统管理员分页查询")
    public Result<PageResult> systemAdminPageQuery(SystemAdminPageQueryDTO systemAdminPageQueryDTO){
        return Result.success(systemAdminService.pageQuery(systemAdminPageQueryDTO));
    }

    @PostMapping("/logout")
    @ApiOperation("系统管理员退出登陆")
    public Result logout(){

        return systemAdminService.logout();
    }

    @PutMapping("/updateStatus")
    @ApiOperation("系统管理员状态更新")
    public Result updateStatus(@RequestBody UpdateStatusDTO updateStatusDTO){
        systemAdminService.updateStatus(updateStatusDTO.getId(), updateStatusDTO.getStatus());
        return Result.success();
    }

    @DeleteMapping("{id}")
    @ApiOperation("系统管理员数据删除")
    public Result deleteSystemAdminById(@PathVariable Long id){
        systemAdminService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询系统管理员数据")
    public Result<SystemAdmin> getSystemAdminById(@PathVariable Long id){
        return Result.success(systemAdminService.getById(id));
    }

    @GetMapping("/listall")
    @ApiOperation("全部系统管理员数据")
    public Result<List<SystemAdmin>> list(){
        return Result.success(systemAdminService.listAll());
    }

}
